package server

import (
	"fmt"
	"net"
	"os"
	"strconv"
)

////测试使用
//func main() {
//	RunTcpServer("127.0.0.1", 8888)
//}

func RunTcpServer(host string, port int) {
	address := host + ":" + strconv.Itoa(port)
	tcpAddr, _ := net.ResolveTCPAddr("tcp", address)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		fmt.Println(err, "tcp listen error")
		os.Exit(1)
	}
	defer listener.Close()
	fmt.Println("server listen on port:" + strconv.Itoa(port))
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			fmt.Println(err, "Waiting conn error")
			continue
		}
		fmt.Println(conn.LocalAddr().String() + " : Client connected!")
		//开启协程来处理接收到conn对象
		go TcpConnectHandler(conn)
	}

}
